﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;                      //Khai bao

namespace Manage_Pupils
{
    public partial class FormCapNhatHS : Form
    { 
        //Ket noi toi database
        private SqlConnection con;
        private DataTable dtHS = new DataTable("tblStudent");//Mo phong cac Table trong CSDL
        private DataTable dtClass = new DataTable("tblClass");
        private SqlDataAdapter daHS = new SqlDataAdapter();  //Quan ly cac ket noi den nguon du lieu
        //Ket noi co so du lieu
        private void connect()
        {
            try
            {
                con = new SqlConnection("Server=IWANTWIN-PC;user id=sa;password=123456;database=QuanLyHocSinh");
                con.Open();

            }
            catch
            {
                MessageBox.Show("Ban da ket noi that bai", "Loi", MessageBoxButtons.OK);
            }
        }
        //Giai phong bo nho
        private void disconnect()
        {
            con.Close();
            con.Dispose();
            con = null;

        }

        //lay ra cac thong tin can thiet tu bang Hoc sinh de gan vao bang tbHocsinh de gan vao datatabble 
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;                             //Ket noi
            //Xac dinh loai cau lenh can lay du lieu,Text: cau lenh SQL(ngam dinh)
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select 
MaHS as N'Ma HS',
HoHS as N'Ho HS',
TenHS as N'Ten HS',
NgaySinh as N'Ngay Sinh',
(Case GioiTinh When '1' Then N'Nam'
               When '0' Then N'Nu'
               else N'Chua xac dinh' end) as N'Gioi Tinh',
DiaChi as N'Dia Chi',
DienThoai as N'Dien Thoai',
MaLop as N'Ma Lop'
               from HocSinh";
            daHS.SelectCommand = command;//Gan command cho da
            daHS.Fill(dtHS);//Nap du lieu cho table
            grvHS.DataSource = dtHS;//Load du lieu len DataGriview
        }

        private void binding()
        {
            textMaHS.Clear();
            textMaHS.DataBindings.Add("Text", grvHS.DataSource, "Ma HS");
            textHoHS.Clear();
            textHoHS.DataBindings.Add("Text", grvHS.DataSource, "Ho HS");
            textTenHS.Clear();
            textTenHS.DataBindings.Add("Text", grvHS.DataSource, "Ten HS");
            textNgaySinh.Clear();
            textNgaySinh.DataBindings.Add("Text", grvHS.DataSource, "Ngay Sinh");
            textGioiTinh.Clear();
            textGioiTinh.DataBindings.Add("Text", grvHS.DataSource, "Gioi Tinh");
            textDiaChi.Clear();
            textDiaChi.DataBindings.Add("Text", grvHS.DataSource, "Dia Chi");
            textDienThoai.Clear();
            textDienThoai.DataBindings.Add("Text", grvHS.DataSource, "Dien Thoai");
            textMaLop.Clear();
            textMaLop.DataBindings.Add("Text", grvHS.DataSource, "Ma Lop");



        }
        public FormCapNhatHS()
        {
            InitializeComponent();
        }
    
        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 fr = new Form1();
            fr.Show();
        }

        private void FormCapNhatHS_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
            disconnect();
        }

        private void buttonThem_Click(object sender, EventArgs e)
        {
            //FormThemHSTrucTiep fr = new FormThemHSTrucTiep();
            //fr.Show();
           // this.Hide();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormThemHSTuExcel fr = new FormThemHSTuExcel();
            fr.Show();
            this.Hide();
        }

        private void buttonXoa_Click(object sender, EventArgs e)
        {

        }

      

      
    }
}
